package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.domain.Menu;
import cn.itcast.domain.Permission;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.service.MenuService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    //远程调用服务
    @Reference
    private MenuService menuService;

    /**
     * 查询角色所对应的菜单
     *
     * @return
     */
    @RequestMapping("/findAllMenus")
    public Result findAllMenus() {

        //获取当前登录的用户名
        //String name = SecurityContextHolder.getContext().getAuthentication().getName();

        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User)
                SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = user.getUsername();
        List<Menu> list = null;
        try {
            list = menuService.findAllMenus(username);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, list);
    }

    /**
     * 新增菜单
     *
     * @param menu
     * @return
     */
    @RequestMapping("/add")
    public Result insertMenu(@RequestBody Menu menu, Integer[] roleIds) {
        try {
            menuService.insertMenu(menu, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            //提示错误信息
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
        //提示成功消息
        return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
    }


    /**
     * 查询单项菜单  回显信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {

        Menu Menu = menuService.findMenuById(id);

        if (Menu == null) {
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        } else {
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, Menu);
        }
    }

    /**
     * 查询菜单对应的角色id
     *
     * @return
     */
    @RequestMapping("/findRoleIds")
    public List<Integer> findRoleIds(Integer id) {
        return menuService.findRoleIds(id);
    }

    /**
     * 分页查询 条件查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult findMenuByPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = menuService.findMenuByPage(queryPageBean);
        return pageResult;
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result deleteMenu(Integer id) {
        try {
            menuService.deleteMenu(id);
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
        //删除成功
        return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
    }

    /**
     * 修改菜单
     *
     * @return
     */
    @RequestMapping("/update")
    public Result updateMenu(@RequestBody Menu menu, Integer[] roleIds) {
        try {
            menuService.updateMenu(menu, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            //编辑 修改失败
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
        //编辑成功
        return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
    }

    /**
     * 回显菜单信息
     *
     * @return
     */
    @RequestMapping("/findAllMenusByRole")
    public Result findAllMenusByRole() {
        List<Menu> list =menuService.findAllMenusByRole();
        if (list == null) {
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        } else {
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, list);
        }
    }
}
